<?php

namespace common\logics;

use Yii;
use common\helpers\Url;
use common\models\trend\TrendLog;
use common\models\user\UserTrend;
use common\models\user\UserTask;
use common\models\trend\TrendRule;

/**
 * 奖励逻辑模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class RewardLogic extends \common\base\Model {

    /**
     * 签到奖励
     * @param type $num
     * @return boolean
     * @throws Exception
     */
    public static function signin($num = false) {
        $rule = TrendRule::findByHandle('signin');
        if (!$rule) {
            return;
        }
        if ($num === false) {
            $num = $rule->exp;
        }
        if (!$num) {
            return;
        }
        $uid = Yii::$app->user->id;
        $mine = UserTrend::findOrNew($uid);
        $exp = 0;
        if (!$mine->isNewRecord) {
            $exp = $mine->experience;
        }
        $mine->experience = $exp + $num;
        $log = new TrendLog();
        $log->attributes = [
            'user_id' => $uid,
            'type' => 'exp',
            'num' => $num,
            'current' => $exp,
            'rule_id' => $rule->id,
            'mark' => $rule->message,
        ];
        $task = $rule->task;

        $transaction = Yii::$app->db->beginTransaction();
        try {
            if (!$log->save()) {
                throw new Exception('经验日志记录失败！');
            }
            if (!$mine->save()) {
                throw new Exception('账户经验更新失败');
            }
            if ($task) {
                $usertask = UserTask::findOrNew([
                    'user_id' => $uid,
                    'task_id' => $rule->task_id,
                    'date' => date('Y-m-d'),
                ]);
                if ($usertask->isNewRecord) {
                    $usertask->times = $task->times;
                }
                $usertask->times -= 1;
                if ($usertask->times <= 0) {
                    $usertask->times = 0;
                    $usertask->status = UserTask::STATUS_ACTIVE;
                }
                if (!$usertask->save()) {
                    throw new Exception('任务关联失败');
                }
            }
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            Yii::error($e->getMessage());
            return false;
        }
        return true;
    }

    /**
     * 点赞三次
     * @param type $num
     * @return boolean
     * @throws Exception
     */
    public static function praise() {
        $rule = TrendRule::findByHandle('praise');
        if (!$rule || !$rule->task) {
            return;
        }
        $task = $rule->task;
        $uid = Yii::$app->user->id;
        $usertask = UserTask::findOrNew([
            'user_id' => $uid,
            'task_id' => $rule->task_id,
            'date' => date('Y-m-d'),
        ]);
        if ($usertask->isNewRecord) {
            $usertask->times = $task->times;
        }
        $usertask->times -= 1;
        if ($usertask->times <= 0) {
            $usertask->times = 0;
        }
        if (!$usertask->save()) {
            return false;
        }
        if ($usertask->times || $usertask->status == UserTask::STATUS_ACTIVE) {
            return;
        }
        $usertask->status = UserTask::STATUS_ACTIVE;
        $num = $rule->exp;
        if (!$num) {
            return;
        }
        $mine = UserTrend::findOrNew($uid);
        $exp = 0;
        if (!$mine->isNewRecord) {
            $exp = $mine->experience;
        }
        $mine->experience = $exp + $num;
        $log = new TrendLog();
        $log->attributes = [
            'user_id' => $uid,
            'type' => 'exp',
            'num' => $num,
            'current' => $exp,
            'rule_id' => $rule->id,
            'mark' => $rule->message,
        ];
        $transaction = Yii::$app->db->beginTransaction();
        try {
            if (!$usertask->save()) {
                throw new Exception('任务完成失败！');
            }
            if (!$log->save()) {
                throw new Exception('经验日志记录失败！');
            }
            if (!$mine->save()) {
                throw new Exception('账户经验更新失败');
            }
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            Yii::error($e->getMessage());
            return false;
        }
        return true;
    }

    /**
     * 发布动态
     * @return boolean
     * @throws Exception
     */
    public static function dynamic() {
        $rule = TrendRule::findByHandle('dynamic');
        if (!$rule || !$rule->task) {
            return;
        }
        $task = $rule->task;
        $uid = Yii::$app->user->id;
        $usertask = UserTask::findOrNew([
            'user_id' => $uid,
            'task_id' => $rule->task_id,
            'date' => date('Y-m-d'),
        ]);
        if ($usertask->isNewRecord) {
            $usertask->times = $task->times;
        }
        $usertask->times -= 1;
        if ($usertask->times <= 0) {
            $usertask->times = 0;
        }
        if (!$usertask->save()) {
            return false;
        }
        if ($usertask->times || $usertask->status == UserTask::STATUS_ACTIVE) {
            return;
        }
        $usertask->status = UserTask::STATUS_ACTIVE;
        $num = $rule->exp;
        if (!$num) {
            return;
        }
        $mine = UserTrend::findOrNew($uid);
        $exp = 0;
        if (!$mine->isNewRecord) {
            $exp = $mine->experience;
        }
        $mine->experience = $exp + $num;
        $log = new TrendLog();
        $log->attributes = [
            'user_id' => $uid,
            'type' => 'exp',
            'num' => $num,
            'current' => $exp,
            'rule_id' => $rule->id,
            'mark' => $rule->message,
        ];
        $transaction = Yii::$app->db->beginTransaction();
        try {
            if (!$usertask->save()) {
                throw new Exception('任务完成失败！');
            }
            if (!$log->save()) {
                throw new Exception('经验日志记录失败！');
            }
            if (!$mine->save()) {
                throw new Exception('账户经验更新失败');
            }
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            Yii::error($e->getMessage());
            return false;
        }
        return true;
    }

    /**
     * 给好有评论
     * @return boolean
     * @throws Exception
     */
    public static function commentFriend() {
        $rule = TrendRule::findByHandle('comment.friend');
        if (!$rule || !$rule->task) {
            return;
        }
        $task = $rule->task;
        $uid = Yii::$app->user->id;
        $usertask = UserTask::findOrNew([
            'user_id' => $uid,
            'task_id' => $rule->task_id,
            'date' => date('Y-m-d'),
        ]);
        if ($usertask->isNewRecord) {
            $usertask->times = $task->times;
        }
        $usertask->times -= 1;
        if ($usertask->times <= 0) {
            $usertask->times = 0;
        }
        if (!$usertask->save()) {
            return false;
        }
        if ($usertask->times || $usertask->status == UserTask::STATUS_ACTIVE) {
            return;
        }
        $usertask->status = UserTask::STATUS_ACTIVE;
        $num = $rule->exp;
        if (!$num) {
            return;
        }
        $mine = UserTrend::findOrNew($uid);
        $exp = 0;
        if (!$mine->isNewRecord) {
            $exp = $mine->experience;
        }
        $mine->experience = $exp + $num;
        $log = new TrendLog();
        $log->attributes = [
            'user_id' => $uid,
            'type' => 'exp',
            'num' => $num,
            'current' => $exp,
            'rule_id' => $rule->id,
            'mark' => $rule->message,
        ];
        $transaction = Yii::$app->db->beginTransaction();
        try {
            if (!$usertask->save()) {
                throw new Exception('任务完成失败！');
            }
            if (!$log->save()) {
                throw new Exception('经验日志记录失败！');
            }
            if (!$mine->save()) {
                throw new Exception('账户经验更新失败');
            }
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            Yii::error($e->getMessage());
            return false;
        }
        return true;
    }

    /**
     * 球迷学院参与闯关
     * @return boolean
     * @throws Exception
     */
    public static function college() {
        $rule = TrendRule::findByHandle('college');
        if (!$rule || !$rule->task) {
            return;
        }
        $task = $rule->task;
        $uid = Yii::$app->user->id;
        $usertask = UserTask::findOrNew([
            'user_id' => $uid,
            'task_id' => $rule->task_id,
            'date' => date('Y-m-d'),
        ]);
        if ($usertask->isNewRecord) {
            $usertask->times = $task->times;
        }
        $usertask->times -= 1;
        if ($usertask->times <= 0) {
            $usertask->times = 0;
        }
        if (!$usertask->save()) {
            return false;
        }
        if ($usertask->times || $usertask->status == UserTask::STATUS_ACTIVE) {
            return;
        }
        $usertask->status = UserTask::STATUS_ACTIVE;
        $num = $rule->exp;
        if (!$num) {
            return;
        }
        $mine = UserTrend::findOrNew($uid);
        $exp = 0;
        if (!$mine->isNewRecord) {
            $exp = $mine->experience;
        }
        $mine->experience = $exp + $num;
        $log = new TrendLog();
        $log->attributes = [
            'user_id' => $uid,
            'type' => 'exp',
            'num' => $num,
            'current' => $exp,
            'rule_id' => $rule->id,
            'mark' => $rule->message,
        ];
        $transaction = Yii::$app->db->beginTransaction();
        try {
            if (!$usertask->save()) {
                throw new Exception('任务完成失败！');
            }
            if (!$log->save()) {
                throw new Exception('经验日志记录失败！');
            }
            if (!$mine->save()) {
                throw new Exception('账户经验更新失败');
            }
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            Yii::error($e->getMessage());
            return false;
        }
        return true;
    }

    /**
     * 拉呱
     * @return boolean
     * @throws Exception
     */
    public static function chat() {
        $rule = TrendRule::findByHandle('chat');
        if (!$rule || !$rule->task) {
            return;
        }
        $task = $rule->task;
        $uid = Yii::$app->user->id;
        $usertask = UserTask::findOrNew([
            'user_id' => $uid,
            'task_id' => $rule->task_id,
            'date' => date('Y-m-d'),
        ]);
        if ($usertask->isNewRecord) {
            $usertask->times = $task->times;
        }
        $usertask->times -= 1;
        if ($usertask->times <= 0) {
            $usertask->times = 0;
        }
        if (!$usertask->save()) {
            return false;
        }
        if ($usertask->times || $usertask->status == UserTask::STATUS_ACTIVE) {
            return;
        }
        $usertask->status = UserTask::STATUS_ACTIVE;
        $num = $rule->exp;
        if (!$num) {
            return;
        }
        $mine = UserTrend::findOrNew($uid);
        $exp = 0;
        if (!$mine->isNewRecord) {
            $exp = $mine->experience;
        }
        $mine->experience = $exp + $num;
        $log = new TrendLog();
        $log->attributes = [
            'user_id' => $uid,
            'type' => 'exp',
            'num' => $num,
            'current' => $exp,
            'rule_id' => $rule->id,
            'mark' => $rule->message,
        ];
        $transaction = Yii::$app->db->beginTransaction();
        try {
            if (!$usertask->save()) {
                throw new Exception('任务完成失败！');
            }
            if (!$log->save()) {
                throw new Exception('经验日志记录失败！');
            }
            if (!$mine->save()) {
                throw new Exception('账户经验更新失败');
            }
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            Yii::error($e->getMessage());
            return false;
        }
        return true;
    }

}
